<?php
/**
 * Pager class. Use this class to make pagination.
 * 
 * @author Eugene I. Nezhuta
 * @copyright (c) 2009 - 2010 NeLab <www.ne-lab.com>
 *
 */

class Pager {
	
	/**
	 * How many items should be on page.
	 * 
	 * @var integer
	 */
    protected $_perPage = 0;
    
    /**
     * All items for pagination
     * 
     * @var integer
     */
    protected $_total = 0;
    
    /**
     * How many links hsould be on pager
     * 
     * @var integer
     */
    protected $_linkPagesCount = 0;
	
    /**
     * All items for pagination
     * 
     * @var array
     */
    protected $_data = array();
	
    /**
     * Set pager options. Pager init.
     * 
     * @param $options array
     * @return void
     */
    public function setOptions($options) {
        if(!is_array($options)) {
            throw new ExceptionCore('Option must be an array');
        }
        $this->_perPage = $options['perPage'];
        $this->_data = $options['data'];
        $this->_total = count($this->_data);
        $this->_linkPagesCount = ceil($this->_total / $this->_perPage);
    }
	
    
    public function getItems($currentPage) {
        $items = array();
        $stop = ($currentPage * $this->_perPage) - 1;
        $start = ($stop - $this->_perPage) + 1;
        for ($i = $start; $i <= $stop; $i++) {
            if(isset($this->_data[$i])) {
            	$items[] = $this->_data[$i];
            }
        }
        return $items;
    }
    
    public function getNextPage($currentPage) {
    	if($currentPage == $this->_linkPagesCount) {
    		return $currentPage;
    	}
    	return ++$currentPage;
    }
    
    public function getPrevPage($currentPage) {
		if($currentPage <= 1) {
			return $currentPage = 1;
		}
		return --$currentPage;
    }
    
    public function getTotal() {
    	return $this->_linkPagesCount;
    }
    
    public function getFirstPage() {
    	return $page = 1;
    }
    
    public function getLastPage() {
    	return $this->_linkPagesCount;
    }

    public function getNearest($currentPage, $direction) {
        $nearestPages = array();
        switch ($direction) {
            case 'right':
                if($currentPage >= $this->_linkPagesCount) {
                    $nearestPages = false;
                }
                else {
                    $low = $currentPage + 1;
                    $high = $low + 3;
                    if(($high > $this->_linkPagesCount)) {
                        $high = $this->_linkPagesCount;
                    }
                    $nearestPages = range($low, $high);
                }
            break;
            case 'left':
                if($currentPage <= 1) {
                    $nearestPages = false;
                }
                else {
                    $low = $currentPage - 1;
                    $high = $low - 3;
                    if($high <= 1) {
                        $high = 1;
                    }
                    $nearestPages = array_reverse(range($low, $high));
                }
            break;
        }
        return $nearestPages;
    }
}
